{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7c026cb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换完成，输出文件：simms-eu-shopify.csv\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import csv\n",
    "\n",
    "# 1. 读取 simms-eu.csv\n",
    "src_file = r'.\\simms-eu.csv'\n",
    "df = pd.read_csv(src_file, dtype=str).fillna(\"\")\n",
    "\n",
    "# 2. Shopify 商品模板字段（可根据你的模板实际字段调整）\n",
    "shopify_columns = [\n",
    "    \"Handle\", \"Title\", \"Body (HTML)\", \"Vendor\", \"Type\", \"Tags\", \"Published\",\n",
    "    \"Option1 Name\", \"Option1 Value\", \"Option2 Name\", \"Option2 Value\", \"Option3 Name\", \"Option3 Value\",\n",
    "    \"Variant SKU\", \"Variant Grams\", \"Variant Inventory Tracker\", \"Variant Inventory Qty\", \"Variant Inventory Policy\",\n",
    "    \"Variant Fulfillment Service\", \"Variant Price\", \"Variant Compare At Price\", \"Variant Requires Shipping\",\n",
    "    \"Variant Taxable\", \"Variant Barcode\", \"Image Src\", \"Image Position\", \"Image Alt Text\", \"Gift Card\", \"SEO Title\",\n",
    "    \"SEO Description\", \"Google Shopping / Google Product Category\", \"Google Shopping / Gender\",\n",
    "    \"Google Shopping / Age Group\", \"Google Shopping / MPN\", \"Google Shopping / AdWords Grouping\",\n",
    "    \"Google Shopping / AdWords Labels\", \"Google Shopping / Condition\", \"Google Shopping / Custom Product\",\n",
    "    \"Google Shopping / Custom Label 0\", \"Google Shopping / Custom Label 1\", \"Google Shopping / Custom Label 2\",\n",
    "    \"Google Shopping / Custom Label 3\", \"Google Shopping / Custom Label 4\", \"Variant Image\", \"Variant Weight Unit\",\n",
    "    \"Variant Tax Code\", \"Cost per item\", \"Status\"\n",
    "]\n",
    "\n",
    "# 3. 转换函数\n",
    "def simms_to_shopify_rows(row):\n",
    "    # 处理图片\n",
    "    images = [img.strip('\"') for img in row['ImagesUrl'].split('\",\"') if img.strip('\"')]\n",
    "    if not images:\n",
    "        images = [\"\"]\n",
    "    # 处理变体\n",
    "    options = []\n",
    "    if row['Attribute 1 name'] and row['Attribute 1 value(s)']:\n",
    "        opt_name = row['Attribute 1 name']\n",
    "        for opt_val in row['Attribute 1 value(s)'].split('|'):\n",
    "            options.append((opt_name, opt_val.strip()))\n",
    "    else:\n",
    "        options.append((\"Title\", \"Default Title\"))\n",
    "\n",
    "    # 展开图片和变体\n",
    "    rows = []\n",
    "    for img_idx, img_url in enumerate(images, 1):\n",
    "        for opt_name, opt_val in options:\n",
    "            shopify_row = {\n",
    "                \"Handle\": row['SKU'].lower().replace(\" \", \"-\"),\n",
    "                \"Title\": row['Name'],\n",
    "                \"Body (HTML)\": row['Description'],\n",
    "                \"Vendor\": \"Simms\",\n",
    "                \"Type\": row['Categories'],\n",
    "                \"Tags\": row.get('Tags', \"\"),\n",
    "                \"Published\": \"TRUE\",\n",
    "                \"Option1 Name\": opt_name,\n",
    "                \"Option1 Value\": opt_val,\n",
    "                \"Option2 Name\": \"\",\n",
    "                \"Option2 Value\": \"\",\n",
    "                \"Option3 Name\": \"\",\n",
    "                \"Option3 Value\": \"\",\n",
    "                \"Variant SKU\": row['SKU'],\n",
    "                \"Variant Grams\": \"\",\n",
    "                \"Variant Inventory Tracker\": \"\",\n",
    "                \"Variant Inventory Qty\": \"100\",\n",
    "                \"Variant Inventory Policy\": \"deny\",\n",
    "                \"Variant Fulfillment Service\": \"manual\",\n",
    "                \"Variant Price\": row['Sale price'] or row['Regular price'],\n",
    "                \"Variant Compare At Price\": row['Regular price'],\n",
    "                \"Variant Requires Shipping\": \"TRUE\",\n",
    "                \"Variant Taxable\": \"TRUE\",\n",
    "                \"Variant Barcode\": \"\",\n",
    "                \"Image Src\": img_url,\n",
    "                \"Image Position\": img_idx,\n",
    "                \"Image Alt Text\": row['Name'],\n",
    "                \"Gift Card\": \"FALSE\",\n",
    "                \"SEO Title\": row['Name'],\n",
    "                \"SEO Description\": \"\",\n",
    "                \"Google Shopping / Google Product Category\": \"\",\n",
    "                \"Google Shopping / Gender\": \"\",\n",
    "                \"Google Shopping / Age Group\": \"\",\n",
    "                \"Google Shopping / MPN\": \"\",\n",
    "                \"Google Shopping / AdWords Grouping\": \"\",\n",
    "                \"Google Shopping / AdWords Labels\": \"\",\n",
    "                \"Google Shopping / Condition\": \"new\",\n",
    "                \"Google Shopping / Custom Product\": \"\",\n",
    "                \"Google Shopping / Custom Label 0\": \"\",\n",
    "                \"Google Shopping / Custom Label 1\": \"\",\n",
    "                \"Google Shopping / Custom Label 2\": \"\",\n",
    "                \"Google Shopping / Custom Label 3\": \"\",\n",
    "                \"Google Shopping / Custom Label 4\": \"\",\n",
    "                \"Variant Image\": img_url,\n",
    "                \"Variant Weight Unit\": \"g\",\n",
    "                \"Variant Tax Code\": \"\",\n",
    "                \"Cost per item\": \"\",\n",
    "                \"Status\": \"active\"\n",
    "            }\n",
    "            rows.append(shopify_row)\n",
    "    return rows\n",
    "\n",
    "# 4. 生成所有行\n",
    "shopify_rows = []\n",
    "for _, row in df.iterrows():\n",
    "    shopify_rows.extend(simms_to_shopify_rows(row))\n",
    "\n",
    "# 5. 写出为shopify格式csv\n",
    "out_file = \"simms-eu-shopify.csv\"\n",
    "with open(out_file, \"w\", newline='', encoding=\"utf-8-sig\") as f:\n",
    "    writer = csv.DictWriter(f, fieldnames=shopify_columns)\n",
    "    writer.writeheader()\n",
    "    for row in shopify_rows:\n",
    "        writer.writerow(row)\n",
    "\n",
    "print(f\"转换完成，输出文件：{out_file}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "90254658",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "900ea778",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\repos\\scripts\\wp\\woocommerce\\woo_df\\csv_dir\\shopyy\\simms\n"
     ]
    }
   ],
   "source": [
    "#打印当前工作目录\n",
    "import os\n",
    "print(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2aff58c0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
